package xxx;
import java.sql.Connection;
class poi{
	private Connection con = null;
	private runSql run = null;
	public poi(Connection c,runSql r){
		this.con = c;
		this.run = r;
	}
	
	public String driverLogin(String name,String phoneNum,String passwd){
		String sql = "select driverID from Driver where phoneNum='"+phoneNum+"' and passwd='"+passwd+"'";
		String id = run.select(con,sql);
		return id;
	}
	public boolean driverRegister(String id,String name,String phoneNum,String palate,String passwd){
		String sql = "insert into Driver(name,phoneNum,passwd,plateNum,lineID) ";
		sql+=" values('"+name+"','"+phoneNum+"','"+passwd+"','"+palate+"',"+id+")";
		int flag = run.update(con,sql);
		return flag==-1 ? false:true;
	}
	//------------------------------------------------------------------------------------------------
	public boolean passengerLoc(String name,String phoneNum,double j,double w,String destination){
		String sql = "delete from PassengerStatus where phoneNum='"+phoneNum+"'";
		int flag = run.update(con,sql);
		sql = "insert into PassengerStatus values('"+name+"','"+phoneNum+"',"+j+","+w+",'"+destination+"',now())";
		flag = run.update(con,sql);
		return flag==-1 ? false:true;
	}
	public boolean driverLoc(String startCity,int driverID,double j,double w){
		String sql = "update DriverStatus set poxJ="+j+",poxW="+w+",time=now() where driverID="+driverID;
		int flag = run.update(con,sql);
		if(flag==1) return true;
		sql = "insert into DriverStatus values('"+startCity+"','"+driverID+"',"+j+","+w+",now())";
		flag = run.update(con,sql);
		return flag==-1 ? false:true;
	}
	public boolean carLoc(String name,String phoneNum,String des,String cbe,double j,double w){
		String sql = "update Car set poxJ="+j+",poxW="+w+",time=now() where phoneNum='"+phoneNum+"'";
		int flag = run.update(con,sql);
		if(flag==1) return true;
		sql = "insert into Car values('"+name+"','"+phoneNum+"','"+des+"','"+cbe+"',"+j+","+w+",now())";
		flag = run.update(con,sql);
		return flag==-1 ? false:true;
	}
	//---------------------------------------------------------------------------------------------------
	public String searchLine(String city){
		String sql = "select cityA,cityB,lineID from Line where cityA like '%"+city+"%' or cityB like '%"+city+"%'";
		String res = run.select(con,sql)+"";
		return res;
	}
	public String getDriver(String startCity,String destination){
		String sql = "select name,Driver.driverID,phoneNum,plateNum,poxJ,poxW from DriverStatus,Line,Driver ";
		 sql+= " where startCity='"+startCity+"' and ";
		sql+="Driver.lineID=Line.lineID and Driver.driverID=DriverStatus.driverID and ";
		sql+=" (cityA='"+startCity+"'"+" and cityB='"+destination+"' ";
		sql+=" or cityB='"+startCity+"'"+" and cityA='"+destination+"')";
		String res = run.select(con,sql)+"";
		return res;
	}
	//---------------------------------------------------------------------------------------------------
	public String getPassenger(String start,int driverID){ //start 为司机的起点
		String sql = "select PassengerStatus.name,PassengerStatus.phoneNum,PassengerStatus.poxJ,";
		sql+="PassengerStatus.poxW,PassengerStatus.destination from PassengerStatus,Driver,Line where  ";
		sql+=" Driver.lineID=Line.lineID and Driver.driverID="+driverID+" and (";
		sql+="(cityA='"+start+"' and destination=cityB) or (cityB='"+start+"' and destination=cityA))";
		String res = run.select(con,sql)+"";
		return res;
	}
	//--------------------------------------------------------------------------------
	public String getCar(String des){
		String sql = "select * from Car where destination='"+des+"'";
		return run.select(con,sql)+"";
	}

	public String searchCar(String des){
		String sql = "select destination from Car where destination like '%"+des+"%'";
		return run.select(con,sql)+"";
	}

	public static void main(String[] args) {
		poi p = new poi(DBBean.getConnection(),new runSql());
		System.out.println(p.searchLine("乌鲁木"));
		
	}

}